#include "gtest/include/gtest/gtest.h"
#include "../../src/common/message/ngmessage.h"
#include "../../src/common/message/ngmessagequeue.h"

class COMMON_NGMessageQueueTest : public ::testing::Test
{
protected:
	void SetUp()
	{
		queue = new NGMessageQueue();

		type1 = 2;
		type2 = 41;
		type3 = 50;
		type4 = 242;
		type5 = 134;

		senderId1 = 12312312;
		senderId2 = 14124;
		senderId3 = 1324;
		senderId4 = 41412;
		senderId5 = 141414;

		strcpy(data1, "hello, world!");
		strcpy(data2, "djasjfsdfas");
		strcpy(data3, "jqlkf qfqfq");
		strcpy(data4, "dajfasdf");
		strcpy(data5, "dasfdfadfasdf");

		msg1 = new NGMessage();
		msg1->setType(type1);
		msg1->setSenderId(senderId1);
		msg1->setData(data1);
		length1 = strlen(data1);

		msg2 = new NGMessage();
		msg2->setType(type2);
		msg2->setSenderId(senderId2);
		msg2->setData(data2);
		length2 = strlen(data2);

		msg3 = new NGMessage();
		msg3->setType(type3);
		msg3->setSenderId(senderId3);
		msg3->setData(data3);
		length3 = strlen(data3);

		msg4 = new NGMessage();
		msg4->setType(type4);
		msg4->setSenderId(senderId4);
		msg4->setData(data4);
		length4 = strlen(data4);

		msg5 = new NGMessage();
		msg5->setType(type5);
		msg5->setSenderId(senderId5);
		msg5->setData(data5);
		length5 = strlen(data5);
	}
	void TearDown()
	{
		delete queue;

		delete msg1;
		delete msg2;
		delete msg3;
		delete msg4;
		delete msg5;
	}

	NGMessageQueue *queue;

	NGMessage *msg1;
	NGMessage *msg2;
	NGMessage *msg3;
	NGMessage *msg4;
	NGMessage *msg5;

	// compare data
	BYTE type1, type2, type3, type4, type5;
	WORD length1, length2, length3, length4, length5;
	DWORD senderId1, senderId2, senderId3, senderId4, senderId5;
	char data1[20], data2[20], data3[20], data4[20], data5[20];
};

// test push() and pop()
TEST_F(COMMON_NGMessageQueueTest, PushPopTest)
{
	ASSERT_EQ(0, queue->size());

	queue->push(msg1);
	queue->push(msg2);

	NGMessage *m = queue->pop();
	ASSERT_EQ(type1, m->getType());
	ASSERT_EQ(senderId1, m->getSenderId());
	ASSERT_STREQ(data1, m->getData());
	ASSERT_EQ(length1, m->getLength());

	m = queue->pop();
	ASSERT_EQ(type2, m->getType());
	ASSERT_EQ(senderId2, m->getSenderId());
	ASSERT_STREQ(data2, m->getData());
	ASSERT_EQ(length2, m->getLength());
}

// test peek()
TEST_F(COMMON_NGMessageQueueTest, PeekTest)
{
	ASSERT_EQ(0, queue->size());

	queue->push(msg1);
	queue->push(msg2);

	ASSERT_EQ(2, queue->size());

	NGMessage *m = queue->peek();
	ASSERT_EQ(type1, m->getType());
	ASSERT_EQ(senderId1, m->getSenderId());
	ASSERT_STREQ(data1, m->getData());
	ASSERT_EQ(length1, m->getLength());

	m = queue->peek();
	ASSERT_EQ(type1, m->getType());
	ASSERT_EQ(senderId1, m->getSenderId());
	ASSERT_STREQ(data1, m->getData());
	ASSERT_EQ(length1, m->getLength());

	m = queue->pop();
	ASSERT_EQ(type1, m->getType());
	ASSERT_EQ(senderId1, m->getSenderId());
	ASSERT_STREQ(data1, m->getData());
	ASSERT_EQ(length1, m->getLength());

	m = queue->pop();
	ASSERT_EQ(type2, m->getType());
	ASSERT_EQ(senderId2, m->getSenderId());
	ASSERT_STREQ(data2, m->getData());
	ASSERT_EQ(length2, m->getLength());
}

// test clear()
TEST_F(COMMON_NGMessageQueueTest, ClearTest)
{
	ASSERT_EQ(0, queue->size());

	queue->push(msg1);
	queue->push(msg2);
	queue->push(msg3);
	ASSERT_EQ(3, queue->size());

	queue->clear();
	ASSERT_EQ(0, queue->size());
}

// test size()
TEST_F(COMMON_NGMessageQueueTest, SizeTest)
{
	// test empty
	ASSERT_EQ(0, queue->size());

	queue->push(msg1);
	queue->push(msg2);

	ASSERT_EQ(2, queue->size());

	queue->pop();

	ASSERT_EQ(1, queue->size());

	queue->push(msg1);
	queue->push(msg2);
	queue->push(msg5);

	ASSERT_EQ(4, queue->size());
}
